<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use App\User as User;

class LoginController extends BaseController
{
		//登陆
	public function login()
	{
		$phone = request("phone");
		$password = request("password", md5(123456));

		$isPhone = $this->prepCheckPhone($phone);
		if($isPhone == 0) {
			return $this->outPutErr('请输入正确的手机号码！');
		}
		
		$User = $this->getUserInfo($phone);
		if($User) {
			if( Hash::check($password, $User->password) ) {

				//获取token令牌
			    $token = $this->getToken($phone, $password);

				return $this->outPutSucc($token);
				// return $this->outPutSucc([
				// 	'userinfo' => $User,
				// 	'token' => $token
				// ]);
			} else {
				return $this->outPutErr('用户名或密码错误！');
			}
		}
		return $this->outPutErr('用户不存在！');
	}

	//注册
	public function register()
	{
		$phone = request("phone");
		$password = request("password",md5(123456));
		$verifyCode = request("verifyCode");
		$isPhone = $this->prepCheckPhone($phone);
		if($isPhone == 0) {
			return $this->outPutErr('请输入正确的手机号码！');
		}

		$key = config("svs.sms_verify")."1".$phone;
		if( !$this->checkVerifyCode($phone, $verifyCode, $key) ){
			return $this->outPutErr('验证码错误！');
		}

		$this->clearCache($key);

		$ext = substr( uniqid(), 7);

		$ret = User::insert([
			'name' => substr($phone,0,7)."_".$ext,
			'phone' => $phone,
			'password' => Bcrypt($password),
			'created_at' => now(),
			'updated_at' => now(),
		]);

		
		if($ret) {
			return $this->outPutSucc();
		} else {
			return $this->outPutErr('网络错误，注册失败！');
		}
	}

		//重置密码
	public function resetUserPassword()
	{
		$phone = request("phone");
		$password = request("password",md5(123456));
		$verifyCode = request("verifyCode");
		$isPhone = $this->prepCheckPhone($phone);
		if($isPhone == 0) {
			return $this->outPutErr('请输入正确的手机号码！');
		}

		$key = config("svs.sms_verify")."2".$phone;
		if( !$this->checkVerifyCode($phone, $verifyCode, $key) ){
			return $this->outPutErr('验证码错误！');
		}

		$this->clearCache($key);

		$ext = substr( uniqid(), 7);

		$user = User::where('phone', $phone)->first();
		$user->password = Bcrypt($password);
		$ret = $user->save();
				
		
		if($ret) {
			return $this->outPutSucc();
		} else {
			return $this->outPutErr('网络错误，操作失败！');
		}
	}

	//获取验证码
	public  function  getVerifyCode()
	{
		$phone = request("phone");
		$verifyType  = request("verifyType", 1);
		
		$isPhone = $this->prepCheckPhone($phone);
		if($isPhone == 0) {
			return $this->outPutErr('请输入正确的手机号码！');
		}

		$User = $this->getUserInfo($phone);
		if($verifyType == 1  && $User) {
			return $this->outPutErr('该手机号已注册！');
		} else if ($verifyType == 2 && !$User) { //重置密码
			return $this->outPutErr('该手机号未注册！请先注册！');
		}
		
		//发送短信，待补充
		$code = mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
		$minutes = 10;

		Cache::put(config("svs.sms_verify").$verifyType.$phone, $code, $minutes);

		return $this->outPutSucc(['code' => $code]);
	}


	//获取用户信息
	public  function  getUserInfo($phone)
	{
		return User::where('phone', $phone)->first();
	}


	//获取token
	protected function getToken($phone, $password)
	{
		//获取token令牌
		$http = new \GuzzleHttp\Client();

	    $response = $http->post(config('app.url').'/oauth/token', [
	        'form_params' => [
	            'grant_type' => 'password',
	            'client_id' => config('svs.client_id'),
	            'client_secret' => config('svs.client_secret'),
	            'username' => $phone,
	            'password' => $password,
	            'scope' => '*',
	        ],
	    ]);

    	return json_decode((string)$response->getBody(), true);
	}
}
